/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.buss.core.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author gykl
 */
public class WebServiceUtils {


    public static Object getSpringContextBean(WebServiceContext context, String beanName){
        MessageContext ctxt = context.getMessageContext();
        ServletContext sc = (ServletContext)ctxt.get(ctxt.SERVLET_CONTEXT);

        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
        Object obj = (Object)ac.getBean(beanName);
        return obj;
    }

    public static String validateUser(String userName,String password){
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream ins = loader.getResourceAsStream("jdbc.properties");
        Properties prop = new Properties();
        try {
            prop.load(ins);
        } catch (IOException ex) {
            Logger.getLogger(WebServiceUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        try{
            String pass = prop.get(userName).toString();
            if(!pass.equals(password)){
                return "你没有权限访问该WebService接口!";
            }
        }catch(Exception ex){
            return "你没有权限访问该WebService接口!";
        }
        return "";
    }
    public static void main(String[] args){
        System.out.println(validateUser("admin","monitor1"));
    }

}
